################################################################################
# Project:  CMake4GDAL
# Purpose:  CMake build scripts
# Author:   Mikhail Gusev, gusevmihs@gmail.com
################################################################################
# Copyright (C) 2015-2016, NextGIS <info@nextgis.com>
# Copyright (C) 2016 Mikhail Gusev
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
################################################################################

find_anyproject(KML DEFAULT ON)
find_anyproject(Boost DEFAULT ON)

CMAKE_DEPENDENT_OPTION(ENABLE_LIBKML "Build LIBKML driver for KML format" ON
"KML_FOUND;BOOST_FOUND" OFF)
if(ENABLE_LIBKML)
    set(TARGET_NAME vec_libkml)

    add_definitions(-DLIBKML_ENABLED)
	set(FORMAT_DEFINITIONS ${FORMAT_DEFINITIONS} "-DLIBKML_ENABLED" PARENT_SCOPE)

    include_directories(${CMAKE_CURRENT_SOURCE_DIR})
    include_directories(${KML_INCLUDE_DIRS})
    include_directories(${UriParser_INCLUDE_DIRS})
    include_directories(${CMAKE_BINARY_DIR}/third-party/install/include)
    include_directories(${CMAKE_SOURCE_DIR}/build/third-party/src/KML_EP/src/kml/base/contrib)

    set(LIB_HHEADERS
    )

    set(LIB_HHEADERS_PRIV
        ogrlibkmlgeometry.h
        ogrlibkmlfield.h
        ogrlibkmlfeature.h
        ogrlibkmlfeaturestyle.h
        ogrlibkmlstyle.h
        ogr_libkml.h
    )

    set(LIB_CSOURCES
        ogrlibkmlgeometry.cpp
        ogrlibkmlfield.cpp
        ogrlibkmlfeature.cpp
        ogrlibkmlfeaturestyle.cpp
        ogrlibkmlstyle.cpp
        ogrlibkmldriver.cpp
        ogrlibkmldatasource.cpp
        ogrlibkmllayer.cpp
    )
    FILE(GLOB extra_dom_src  ${CMAKE_SOURCE_DIR}/build/third-party/src/KML_EP/src/kml/dom/*.cc)
    FILE(GLOB extra_engine_src  ${CMAKE_SOURCE_DIR}/build/third-party/src/KML_EP/src/kml/engine/*.cc)
    FILE(GLOB extra_uiparser_src  ${CMAKE_SOURCE_DIR}/build/third-party/src/UriParser_EP/src/*.cc)


#    set(extra_src ${CMAKE_SOURCE_DIR}/build/third-party/src/KML_EP/src/kml/base/math_util.cc
#            ${CMAKE_SOURCE_DIR}/build/third-party/src/KML_EP/src/kml/base/attributes.cc
#            ${CMAKE_SOURCE_DIR}/build/third-party/src/KML_EP/src/kml/base/string_util.cc
#            ${CMAKE_SOURCE_DIR}/build/third-party/src/KML_EP/src/kml/base/expat_handler_ns.cc
#            ${CMAKE_SOURCE_DIR}/build/third-party/src/KML_EP/src/kml/base/expat_parser.cc
#            ${CMAKE_SOURCE_DIR}/build/third-party/src/KML_EP/src/kml/base/file_posix.cc
#            ${CMAKE_SOURCE_DIR}/build/third-party/src/KML_EP/src/kml/base/file.cc
#            ${CMAKE_SOURCE_DIR}/build/third-party/src/KML_EP/src/kml/base/file_win32.cc
#            ${CMAKE_SOURCE_DIR}/build/third-party/src/KML_EP/src/kml/base/date_time.cc
#            ${CMAKE_SOURCE_DIR}/build/third-party/src/KML_EP/src/kml/base/csv_splitter.cc
#            ${CMAKE_SOURCE_DIR}/build/third-party/src/KML_EP/src/kml/base/referent.cc
#            ${CMAKE_SOURCE_DIR}/build/third-party/src/KML_EP/src/kml/base/time_util.cc
#            ${CMAKE_SOURCE_DIR}/build/third-party/src/KML_EP/src/kml/base/version.cc
##            ${CMAKE_SOURCE_DIR}/build/third-party/src/KML_EP/src/kml/base/uri_parser.cc
#            ${CMAKE_SOURCE_DIR}/build/third-party/src/KML_EP/src/kml/base/xml_namespaces.cc
#            ${CMAKE_SOURCE_DIR}/build/third-party/src/KML_EP/src/kml/base/zip_file.cc
#            ${CMAKE_SOURCE_DIR}/build/third-party/src/KML_EP/src/kml/base/contrib/strptime.c
#            ${CMAKE_SOURCE_DIR}/build/third-party/src/KML_EP/src/kml/base/contrib/minizip/ioapi.c
#            ${CMAKE_SOURCE_DIR}/build/third-party/src/KML_EP/src/kml/base/contrib/minizip/iomem_simple.c
#            ${CMAKE_SOURCE_DIR}/build/third-party/src/KML_EP/src/kml/base/contrib/minizip/unzip.c
#            ${CMAKE_SOURCE_DIR}/build/third-party/src/KML_EP/src/kml/base/contrib/minizip/zip.c
#            ${CMAKE_SOURCE_DIR}/build/third-party/src/KML_EP/src/kml/convenience/feature_list.cc
#            ${CMAKE_SOURCE_DIR}/build/third-party/src/KML_EP/src/kml/convenience/convenience.cc
#            #        ${PROJECT_SOURCE_DIR}/src/kml/dom/kml_factory.cc
#            ${extra_dom_src}
#            ${extra_engine_src}
#            )

    add_library(${TARGET_NAME} OBJECT ${LIB_HHEADERS} ${LIB_HHEADERS_PRIV} ${LIB_CSOURCES} ${extra_src} ${extra_uiparser_src})

#    target_link_libraries(${TARGET_NAME} ${UriParser_LIBRARIES})

    # build target
    set(TARGET_OBJECTS ${TARGET_OBJECTS} $<TARGET_OBJECTS:${TARGET_NAME}> PARENT_SCOPE)
    set(TARGET_LINK_LIB ${TARGET_LINK_LIB} PARENT_SCOPE)
    set(DEPENDENCY_LIB ${DEPENDENCY_LIB} PARENT_SCOPE)
else()
    warning_msg("Skip libkml")
endif()
